import { queryTableDataReq, querySysOperTypeReq } from "./service";
import { message } from "antd";
import { formatDict } from "@/utils/format";
export default {
  namespace: "SystemOperateLog",
  state: {
    init: false,
    count: 0,
    total: "",
    pageNumber: 1,
    tableData: [],
    sysOperTypeList: [],
    formParams: {},
  },
  effects: {
    *pageInit({}, { put }) {
      yield put({ type: "queryTableData" });
      // yield put({ type: 'querySysOperType' });
    },

    //页码操作处理
    *queryTableData({ params }, { put, select }) {
      const { formParams } = yield select((state) => state.SystemOperateLog);
      let _params = {
        pageSize: 10,
      };
      Object.assign(
        _params,
        params?.pageNum && formParams,
        formParams?.pageSize && { pageSize: formParams.pageSize },
        formParams?.pageNum && { pageNum: formParams.pageNum },
        params,
        !params?.pageNum && { pageNum: 1 }
      );
      const res = yield queryTableDataReq({
        ..._params,
        current: _params.pageNum,
      });
      if (res.code === 200) {
        yield put({
          type: "save",
          payload: {
            tableData: res.data.records,
            total: res.data.total,
            formParams: _params,
            pageNumber: params?.pageNum,
            pageSize: params?.pageSize,
          },
        });
      } else {
        message.error(msg);
      }
    },
    //操作项目
    *querySysOperType({ params }, { call, put }) {
      const { code, data } = yield call(querySysOperTypeReq, params);
      if (code === 200) {
        yield put({
          type: "save",
          payload: {
            sysOperTypeList: formatDict(data, "dictLabel", "dictValue"),
          },
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
